import {
  getDataList, getItemList, editData, getCateDataList, getModelList, removeDataModel, addData, readModel, editDataModel
} from '@/api/file'

const state = {
  showUploadBox: false,
  count: +'',
  tableData: [],
  showDeleteBox: false,
  // 上传弹框内得项目列表
  uploadItemList: [],
  // 是否展示编辑盒子
  showEditBox: false,
  itemList: [],
  // 是否展示添加类目盒子
  showAddItem: false,
  // 是否展示删除类目盒子
  showDeleteItemBox: false,
  cateDataList: [],
  cateListCount: +'',
  dataList: [], // 数据模型表格数据
  dataCount: +''
}

const mutations = {
  // 是否展示更新盒子
  show_UploadBox (state, boolean) {
    state.showUploadBox = boolean
  },
  // 设置更新列表
  update_list (state, tableData) {
    state.tableData = tableData
  },
  // 总数
  list_count (state, count) {
    state.count = count
  },
  selectItem_list (state, uploadItemList) {
    state.uploadItemList = uploadItemList
  },
  show_editBox (state, boolean) {
    state.showEditBox = boolean
  },
  select_list (state, itemList) {
    state.itemList = itemList
  },
  show_addItemBox (state, boolean) {
    state.showAddItem = boolean
  },
  show_DeleteBox (state, boolean) {
    state.showDeleteBox = boolean
  },
  show_DeleteItemBox (state, boolean) {
    state.showDeleteItemBox = boolean
  },
  update_cateList (state, cateDataList) {
    state.cateDataList = cateDataList
  },
  cateList_count (state, count) {
    state.cateListCount = count
  },
  update_dataList (state, dataList) {
    state.dataList = dataList
  },
  update_dataCount (state, count) {
    state.dataCount = count
  }

}

const actions = {
  // 刷新列表
  updateTableList (context, params) {
    return new Promise((resolve, reject) => {
      getDataList(params).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_list', res.data.data.data)
          context.commit('list_count', res.data.data.count)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  // 获取类目选项
  getdataItemsList (context, params) {
    return new Promise((resolve, reject) => {
      getItemList(params).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('selectItem_list', res.data.data.filter(item => item.name !== '全部'))
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  upDateTableData (context, params) {
    return new Promise((resolve, reject) => {
      editData(params).then(res => {
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  updateCateDataList (context, params) {
    return new Promise((resolve, reject) => {
      getCateDataList(params).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_cateList', res.data.data.data)
          context.commit('cateList_count', res.data.data.count)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  upDateDataList (context, params) {
    return new Promise((resolve, reject) => {
      getModelList(params).then(res => {
        if (res.data.code === (200 || '200')) {
          context.commit('update_dataList', res.data.data.data)
          context.commit('update_dataCount', res.data.data.count)
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  RemoveDataModel (context, params) {
    return new Promise((resolve, reject) => {
      removeDataModel(params).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  addModelData (context, params) {
    return new Promise((resolve, reject) => {
      addData(params).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  readModelData (context, params) {
    return new Promise((resolve, reject) => {
      readModel(params).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  },
  editModelData (context, params) {
    return new Promise((resolve, reject) => {
      editDataModel(params).then(res => {
        if (res.data.code === (200 || '200')) {
        }
        resolve(res)
      })
        .catch(err => {
          reject(err)
        })
    })
  }
  // async getItemList (context, id) {
  //   const res = await getItemList(id)
  //   context.commit('select_list', res.data.data)
  // }
}
export default {
  namespaced: true,
  actions,
  state,
  mutations
}
